package com.zyg.user.service.Impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyg.user.dao.TbUserMapper;
import com.zyg.user.entity.TbUser;
import com.zyg.user.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private TbUserMapper userMapper;
    //生成验证码,同时发送信息给阿里大于(利用rabbitMQ发送消息)
    //将生成的验证码放到redis中(以手机号为key)
    @Override
    public void createCode(String phone) {
        //生成验证码
        int min = 100000;   //六位数的最小值
        int max = 999999;   //六位数的最大值
        Random random = new Random();
        int code = random.nextInt(max-min);  //得到一个[0,max-min)范围的随机数
        if(code < min){
            code = code + min ;
        }
        System.out.println("生成的六位数的验证码是：" + code);

        //将随机数保存到redis中
        redisTemplate.opsForValue().set(phone,code+"",10, TimeUnit.DAYS);
        //通过rabbitmq发送消息给消息的监听方
        //组织数据
        Map<String, String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code+"");
        //发送数据
        rabbitTemplate.convertAndSend("sms",map);
    }
    //校验验证码
    @Override
    public boolean isExistsCode(String validcode, String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if (StrUtil.isNotBlank(code) && code.equals(validcode)){
            return true;
        }
        return false;

    }

    //添加用户
    @Override
    public void add(TbUser user) {
        System.out.println("user(userServiceImpl) => " + user);
        //加密密码
        String s = SecureUtil.md5(user.getPassword());
        //重写设置回去
        user.setPassword(s);
        //保存用户
        userMapper.insert(user);
    }

    //根据用户id查询用户对象
    @Override
    public TbUser findById(Long id) {
        return userMapper.selectById(id);
    }

    //根据uid查询用户是否存在
    @Override
    public TbUser findByUid(String uid) {
        return userMapper.selectOne(new QueryWrapper<TbUser>().eq("uid",uid));
    }

    //修改用户
    @Override
    public void update(TbUser user) {
        userMapper.updateById(user);
    }
}
